﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Products;
using MinhTuan;

public partial class admin_order_order_Import : System.Web.UI.Page
{
       
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            //if (Request.Cookies["thegioidienthoai"] != null)
            //{
            //    Response.Cookies["thegioidienthoai"]["order"] = null;
            //}
            Bind();
        }
    }
    protected void Bind()
    {
        this.dropncc.DataSource = Supply.GetAll();
        this.dropncc.DataBind();
        this.DropTensp.DataSource = Product.GetAll();
        this.DropTensp.DataBind();
    }
    protected void Lisproducts_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {

    }
    protected void btnthem_Click(object sender, EventArgs e)
    {

        if (Request.Cookies["thegioidienthoai"]!= null)
        {
            SelectedList listproduct = new SelectedList();
            SelectedProduct iitem;        
            String items = Request.Cookies["thegioidienthoai"]["order"].ToString();
            String item = "-"+ DropTensp.SelectedValue.ToString() + "," + DropTensp.SelectedItem.Text+ "," + txtsoluong.Text + "," + txtgia.Text;  
            Response.Cookies["thegioidienthoai"]["order"] =items+item;

            String aa = items+item;
            String[] eitems=aa.Split('-');
            //String[] eitems = Request.Cookies["thegioidienthoai"]["order"].ToString().Split('-');
            for (int i = 0; i <eitems.Length; i++)
            {
               
                String[] product = eitems[i].Split(',');
                iitem = new SelectedProduct(int.Parse(product[0]),product[1],"Cái",int.Parse(product[2]),Decimal.Parse(product[3]));
                listproduct.Add(iitem);
            
            }
            //Label1.Text = Request.Cookies["thegioidienthoai"]["order"].ToString();//SelectedList.UpdateOrdercart(listproduct)+"//"+eitems.Length.ToString();
            this.Lisproducts.DataSource = listproduct.Items;
            this.Lisproducts.DataBind();
        }
        else
        {
            SelectedList listproduct = new SelectedList();
            SelectedProduct iitem;
            String temp = DropTensp.SelectedValue.ToString() + "," + DropTensp.SelectedItem.Text + "," + txtsoluong.Text + "," + txtgia.Text;
            HttpCookie Vtsg = new HttpCookie("thegioidienthoai");
            Vtsg.Values.Add("order",temp);
            Vtsg.Values.Add("count", "1");
            Vtsg.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(Vtsg);
            iitem = new SelectedProduct();
            //iitem.Id = int.Parse(txtid.Text);
            //iitem.Name = txtten.Text;
            iitem.Id = int.Parse(DropTensp.SelectedValue.ToString());
            iitem.Name = DropTensp.SelectedItem.Text;
            iitem.Unit = "Cái";
            iitem.Price = int.Parse(txtgia.Text);
            iitem.Quantity = int.Parse(txtsoluong.Text);
            listproduct.Add(iitem);
            this.Lisproducts.DataSource = listproduct.Items;
            this.Lisproducts.DataBind();
        }
    }
    protected void btnlamlai_Click(object sender, EventArgs e)
    {
        Response.Cookies["thegioidienthoai"]["order"] = "";
        Response.Cookies["thegioidienthoai"].Expires = DateTime.Now.AddDays(-1);
        
        this.Lisproducts.Items.Clear();
        this.Lisproducts.DataBind();
    }
    protected void btnluu_Click(object sender, EventArgs e)
    {
        //if (Lisproducts.Items.Count > 0)
        //{
            SelectedProduct slp;
            OrderImport orderI = new OrderImport();
            orderI.People_Name = "Tuan";
            orderI.Supply_Id =int.Parse(dropncc.SelectedValue.ToString());
            orderI.Order_Date = DateTime.Now;
            orderI.Notice = "Không có ghi chú";
            String[] cartinfo;
            CartItem myCartItem = new CartItem();
            HiddenField hdinfo;
            foreach (ListViewItem myItem in this.Lisproducts.Items)
            {
                if (myItem.ItemType == ListViewItemType.DataItem)
                {
                    hdinfo = (HiddenField)myItem.FindControl("hdinfo");
                    if (hdinfo != null)
                    {
                        cartinfo = hdinfo.Value.ToString().Split(',');
                        slp = new SelectedProduct(Convert.ToInt16(cartinfo[0]),cartinfo[1],"Cái",Convert.ToInt16(cartinfo[2]),Convert.ToInt16(cartinfo[3]));
                        orderI.Item.Add(slp);

                    }
                }
            }
            Int32 result = Product.AddOrderImport(orderI);
            if (result > 0) Label1.Text = "them thanh cong";
            else Label1.Text = "them that bai";
        //}
    }
}
